<template>
  <div class="image">
    <el-form ref="form" :model="form" label-width="130px">
      <el-form-item  :label="$t('SerchImage_Mirrorsource')" prop="imageUrl">
        <el-select v-model="form.imageUrl" placeholder="$t('SerchImage_Pleaseselectimagesource')">
          <el-option label="DockerHub" value=""></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('SerchImage_Pleaseselectimagesource')"   prop="imageName"
      :rules="[
        { required: true, message: $t('SerchImage_Pleaseinputkeywords')}
      ]"
      >
        <el-input v-model.trim="form.imageName" :placeholder="$t('SerchImage_Pleaseinputkeywords')" style="width: 90%" @keyup.enter.native="onSubmit"></el-input>
      </el-form-item>
      <el-form-item  :label="$t('SerchImage_Limitquantity')" prop="limt">
        <el-input v-model.trim="form.limt" type="number" :placeholder="$t('SerchImage_Limithowmanyimagestodisplay')" @keyup.enter.native="onSubmit" style="width: 30%"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button style="background: #067FB5; color: #fff;" size="small" @click="onSubmit">{{$t('SerchImage_Searchimage')}}</el-button>
        <el-button type="warning" size="small" v-if="imageList.length > 0" @click="closeList">{{$t('SerchImage_Closelist')}}</el-button>
      </el-form-item>
    </el-form>
    <ul class="list" v-if="imageList.length > 0">
      <li>
        <span>{{$t('SerchImage_Pleaseselectimagesource')}}</span>
        <span>{{$t('SerchImage_Star')}}</span>
        <span>{{$t('SerchImage_describe')}}</span>
        <span>{{$t('SerchImage_operation')}}</span>
      </li>
      <li v-for="item in imageList" :key="item.name">
        <span :title="item.name">{{item.name}}</span>
        <span :title="item.star_count">{{item.star_count}}</span>
        <span :title="item.description">{{item.description}}</span>
        <span>
          <el-button style="background: #067FB5; color: #fff;" size="mini" @click="pullImage(item.name)">{{$t('SerchImage_Pull')}}</el-button>
        </span>
      </li>
    </ul>
  </div>
</template>

<script>
import post from '@/utils/post'
import http from '@/utils/http'
export default {
  data () {
    return {
      form: { // 表单数据绑定
        imageUrl: '',
        imageName: '',
        limt: 10
      },
      imageList: [] // 请求回来的image数据
    }
  },
  methods: {
    // 关闭镜像列表
    closeList () {
      this.form.imageName = ''
      this.form.limt = 10
      this.imageList = []
    },
    // 搜索镜像
    onSubmit () {
      if (!this.form.imageName) {
        return this.$message({
          showClose: true,
          message: '搜索关键字不能为空',
          type: 'error'
        })
      }
      if (!this.form.imageName) {
        this.form.imageName = 10
      }
      this.loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      const docker = `curl --unix-socket /var/run/docker.sock -s "http:/v1.40/images/search?term=${this.form.imageName}&limit=${this.form.limt}"` // 搜索镜像
      post({ docker }, (res) => {
        this.loading.close()
        if (res.length === 0) {
          return this.$message({
            showClose: true,
            message: '搜索关键字没有匹配镜像',
            type: 'error'
          })
        }
        this.imageList = res
      }, this)
    },
    // 拉取取镜像，默认latest
    pullImage (name) {
      this.loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      const docker = `curl --unix-socket /var/run/docker.sock -s -X POST "http:/v1.40/images/create?fromImage=${name}:latest"` // 拉取镜像
      http({
        url: '/docker',
        method: 'POST',
        timeout: 600000000,
        data: { docker }
      }).then(res => {
        this.loading.close()
        if (res.Code !== 200) {
          return this.$message({
            showClose: true,
            message: '镜像拉取失败！',
            type: 'error'
          })
        }
        this.$message({
          showClose: true,
          message: '镜像拉取成功！',
          type: 'success'
        })
        this.closeList()
        this.$emit('Success')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.list{
  margin: 0px 20px 20px 20px;
  font-size: 14px;
  border: solid 1px hsl(0, 0%, 86%);
  border-radius: 5px;
  color: #333;
  li{
    line-height: 40px;
    display: flex;
    justify-content: space-around;
    align-items: center;
    border-bottom: 1px solid #dbdbdb;
    span{
      flex: 2;
      text-align: center;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    span:nth-child(2),
    span:nth-child(2){
      flex: 1;
    }
  }
  li:nth-child(1){
    font-size: 14px;
    font-weight: bold;
  }
}
</style>
